﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.cms.presentation.Trees;
using System.Text;
using umbraco.BusinessLogic.Actions;
using umbraco.interfaces;
using Procure.Data.Context;

namespace Procure.Web.productType
{
    public class loadProductType : BaseTree
    {
        public loadProductType(string application) : base(application) { }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = TreeAlias;
            rootNode.NodeID = "init";


        }

        protected override void CreateRootNodeActions(ref List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionNew.Instance);
            actions.Add(ActionRefresh.Instance);
        }

        protected override void CreateAllowedActions(ref List<IAction> actions)
        {
            actions.Clear();
            //actions.Add(ActionRefresh.Instance);
            actions.Add(ActionDelete.Instance);
            
        }

        public override void Render(ref XmlTree tree)
        {

            XmlTreeNode xNode = null;
            try
            {
                ProcureDataContext dataStore = DataFactory.GetDataContext();
                var productTypes = from dataType in dataStore.ProductTypes
                                   orderby dataType.Name
                                   select dataType;

                foreach (var productType in productTypes)
                {
                    xNode = XmlTreeNode.Create(this);
                    xNode.NodeID = productType.Id.ToString();
                    xNode.Text = productType.Name;
                    xNode.Action = string.Format("javascript:openProductType('{0}');", productType.Id);
                    xNode.Icon = "productType.gif";
                    tree.Add(xNode);
                }
            }
            catch (Exception) { }
        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append(
               @"
                    function openProductType(id) {
	                    parent.right.document.location.href = 'Procure/editProductType.aspx?id=' + id;
                    }
                ");
        }

    }
}